import axios from "axios";

module.exports = function(Mogo) {
  const State = Mogo.State;
  // axios.defaults.baseURL = Mogo._config.domain;
  axios.defaults.headers.post["Content-Type"] =
    "application/x-www-form-urlencoded";

  Mogo.request = async function(data) {
    const userState = State.get("mogoUser");
    let token = "";
    if (userState && userState.token) {
      token = userState.token;
    }
    try {
      const conf = {
        headers: {
          Authorization: token
        },
        baseURL: Mogo._config.domain,
        ...data
      };
      const res = await axios(conf);
      return res.data;
    } catch (error) {
      // 触发报错器
      Mogo._config.ajaxErrorHandel && Mogo._config.ajaxErrorHandel(error);
      return await Promise.reject(error.response);
    }
  };
};
